﻿using System;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
using FluentNHibernate;
using Webshop20.Domain.Catalog.Core;

namespace Webshop20.Infrastructure.Persistence.NH.AutoMappings.Conventions.Particular
{
    public class ShoppingCartConventions : IClassConvention
    {
        public bool Accept(IClassMap target)
        {
            return target.EntityType.Name == "ShoppingCart";
        }

        public void Apply(IClassMap target)
        {
            var autoMap = (AutoMap<ShoppingCart>)target;

            autoMap.HasMany<ShoppingCartItem>(Reveal.Property<ShoppingCart>("shoppingCartItems")).KeyColumnNames.Add("ShoppingCartId").Cascade.AllDeleteOrphan();
            autoMap.HasMany<SavedItem>(Reveal.Property<ShoppingCart>("savedItems")).KeyColumnNames.Add("ShoppingCartId").Cascade.AllDeleteOrphan();
            
        }


    }
}
